//定义影院相关的接口
const express = require('express')
const router = express.Router()
const response = require('../pool/Response')
//引入mysql连接池
const pool = require('../pool/pool')
//引入joi模块
const joi = require('joi')

//添加影院接口
router.post('/cinema/add',(req,res) => {
    let {cinema_name,address,province,city,district,longitude,latitude,tags} = req.body
    let schema = joi.object({
        cinema_name:joi.string().required(),
        address:joi.string().required(),
        province:joi.string().required(),
        city:joi.string().required(),
        district:joi.string().required(),
        longitude:joi.string().required(),
        latitude:joi.string().required(),
        tags:joi.string().required(),
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query(
        'insert into movie_cinema (cinema_name,address,province,city,district,longitude,latitude,tags) values(?,?,?,?,?,?,?,?)',
    [cinema_name,address,province,city,district,longitude,latitude,tags],(err,result)=>{
        if(err){
            res.send(response.error(500,err))
            throw err;
        }
        res.send(response.ok())
    })
})

//影城标签列表
router.get('/cinema/tags',(req,res) => {
    pool.query('select * from movie_cinema_tag',(error,result) => {
        if(error){
            res.send(response.error(error))
            throw error
        }
        res.send(response.ok(result))
    })
})
//查询影城接口
router.get('/cinemas',(req,res) => {
    pool.query('select * from movie_cinema',(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok(result))
    })
})

//删除电影院
router.post('/cinema/del',(req,res) => {
    let {id} = req.body
    //表单验证
    let schema = joi.object({
        id:joi.string().required()
    })
    let {error,value} =schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return
    }
    pool.query('delete from movie_cinema where id = ?',[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error;
        }
        res.send(response.ok())
    })
})

//通过ID查询影院信息
router.get('/cinema/query',(req,res) => {
    let {id} = req.query
    //表单验证
    let schema = joi.object({
        id:joi.string().required() //必填
    })
    let {error,value} = schema.validate(req.query)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query('select * from movie_cinema where id =?',[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok(result[0]))
    })
})
//修改电影院
router.post('/cinema/update',(req,res) => {
    let {id,cinema_name,address,province,city,district,longitude,latitude,tags} = req.body
    let schema = joi.object({
        id:joi.string().required(),
        cinema_name:joi.string().required(),
        address:joi.string().required(),
        province:joi.string().required(),
        city:joi.string().required(),
        district:joi.string().required(),
        longitude:joi.string().required(),
        latitude:joi.string().required(),
        tags:joi.string().required(),
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query(
        `update movie_cinema set
        cinema_name=?,
        address=?,
        province=?,
        city=?,
        district=?,
        longitude=?,
        latitude=?,
        tags=?
        where id =?
        `,
    [cinema_name,address,province,city,district,longitude,latitude,tags,id],(err,result)=>{
        if(err){
            res.send(response.error(500,err))
            throw err;
        }
        res.send(response.ok())
    })
})
module.exports = router